package com.binest.common.utils;

import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

import java.math.BigInteger;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: ChainUtils
 * @Description:
 * @Author
 * @Date 2024/3/4
 * @Version 1.0
 */
@Slf4j
@Component
public class ChainUtils {
    private static final String DATA_PREFIX = "0x70a08231000000000000000000000000";
    public static final String APPROVE_TOPIC = "0x7988f9fb70d87f2066f20def1f88068b13a7e03b4d39879881c42be365771186";
    public static final String CLAIM_TOPIC = "0x2ecee2bcd33b63158b60c5ee91d7494aa423d23ac493d1a375ce87cd51abc8f3";
    private static Web3j web3j;
    public static String nodeURL;
    public static String playerContract;
    public final static BigInteger scanSpace = new BigInteger("200");
    public static String getNodeURL() {
        return nodeURL;
    }
    @Value(value = "${chain.NodeURL}")
    public void setEthNodeURL(String nodeURL) {
        ChainUtils.nodeURL = nodeURL;
    }

    public static String getPlayerContract() {
        return playerContract;
    }

    @Value(value = "${chain.playerContract}")
    public void setPlayerContract(String playerContract) {
        ChainUtils.playerContract = playerContract;
    }
    private static final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(1800, TimeUnit.SECONDS)
            .writeTimeout(1800, TimeUnit.SECONDS)
            .readTimeout(1800, TimeUnit.SECONDS)
            .build();
    public static Web3j getWeb3j(){
        if(web3j==null){
            web3j = Web3j.build(new HttpService(nodeURL, client, false));
        }
        return web3j;
    }
}
